package com.franklin.validation.core.argument.impl;

import com.franklin.validation.core.annotations.ParamCheck;
import com.franklin.validation.core.argument.interfaces.ErrorResult;
import com.franklin.validation.core.argument.interfaces.ResolverResult;
import com.franklin.validation.core.constants.Constants;
import com.franklin.validation.core.entity.CheckFormEntity;
import com.franklin.validation.core.entity.CheckResult;
import com.franklin.validation.core.exception.ParamCheckException;
import com.franklin.validation.core.utils.DataCheckUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ConfigurableApplicationContext;

import javax.annotation.PostConstruct;

/**
 * Description : 结果抽象父类
 * Create By: Franklin
 * Date : 2020/6/15 14:30
 */
public abstract class AbstractResolverResult<R> implements ResolverResult<CheckFormEntity,CheckResult,R>,ErrorResult<CheckFormEntity,R> {

    @Override
    public abstract CheckResult check(CheckFormEntity input);

    protected void getError(ParamCheck paramCheck){
        if (DataCheckUtil.isNotEmpty(paramCheck.message())){
            throw new ParamCheckException(Constants.MESSAGE + paramCheck.message());
        }
        throw new ParamCheckException(paramCheck.value());
    }

    protected String getErrorMessage(ParamCheck paramCheck){
        try {
            this.getError(paramCheck);
        }catch (ParamCheckException e){
            return e.getMessage();
        }
        return Constants.EMPTY;
    }
}
